package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import com.itheima.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService es;


    /**
     * 根据id查询员工
     */
    @GetMapping("/{id}")
   public R<Object> updateEmployee(@PathVariable Integer id ){
        System.out.println("id =="+ id);
        Employee byIdEmployee = es.getByIdEmployee(id);
        if (byIdEmployee == null){
         return R.error("没有查询到员工");



















        }
         return  R.success(byIdEmployee);
    }


    /**
     * 修改员工状态
     */
    @PutMapping()
    public R<Object> updateByIdEmployeeStatus(HttpServletRequest request,
                                         @RequestBody Employee
                                          employee){

        Integer id = (Integer) request.getSession().getAttribute("id");
        System.out.println("当前登录ID="+id);
        System.out.println("要修改的ID="+employee.getId());
        System.out.println("当前状态="+employee.getStatus());

        Integer result = es.updateEmployeeStatus(employee);
       if (result ==null){
          return R.error("修改失败");
       }

    return R.success("修改成功");
    }


    /**
     * 显示员工
     */
    @GetMapping("/page")
    public R<Object> getEmployee( Integer page,
                          Integer pageSize,
                          String name){

        System.out.println(page+"-----"+pageSize+"---"+name);
        IPage<Employee> employees = es.getEmployee(page, pageSize,name);
        if (employees == null){
            R.error("查询失败");
        }
        return  R.success(employees);

    }

    /**
     * 新增员工
     */

    @PostMapping
    public R<Object> addEmployee(HttpServletRequest request, @RequestBody Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));//死密码
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        Integer id =(Integer) request.getSession().getAttribute("id");

        employee.setCreateUser(id);
        employee.setUpdateUser(id);

        System.out.println(employee);
        Integer result = es.addEmployee(employee);
        if (result == null) {
           return R.error("添加失败");
        }
        else{
            return  R.success("添加成功");
        }

    }




    /**
     * 登录
     *
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Object> login(@RequestBody Employee employee, HttpServletRequest request) {

        Employee resultEmployee = es.login(employee);
        System.out.println(resultEmployee);
        if (resultEmployee == null) {
            return R.error("登录失败,resultEmployee为null");
        } else {
            if (resultEmployee.getStatus() == 0) {

                return R.error("该账号已禁用");
            } else {
                request.getSession().setAttribute("id",resultEmployee.getId());

                return R.success(resultEmployee);
            }

        }
    }

    /**
     * 退出
     */
    @PostMapping("/logout")
    public R<Object> exit(HttpServletRequest request) {

        request.getSession().removeAttribute("id");

        return R.success("退出成功");
    }

}
